/*
 * Copyright (C) 2011 flashflexpro@gmail.com  All Rights Reserved.  No
 *   use, copying or distribution of this work may be made except in
 *   accordance with a valid license agreement from flashflexpro@gmail.com
 *   This notice must be included on all copies, modifications and
 *   derivatives of this work.
 *
 *   flashflexpro@gmail.com MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT
 *   THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
 *   INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 *   NON-INFRINGEMENT. flashflexpro@gmail.com SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
 *   BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 *   SOFTWARE OR ITS DERIVATIVES.
 */

package com.flashflexpro.as3lib.tree {

import flash.geom.Rectangle;

import mx.events.DragEvent;

import spark.layouts.VerticalLayout;
import spark.layouts.supportClasses.DropLocation;

[Bindable]
public class ListTreeLayout extends VerticalLayout {
    public function ListTreeLayout() {
        super();
    }

    public var listTree:ListTree;


    override protected function calculateDropIndex(x:Number, y:Number):int {
        var rt:int = super.calculateDropIndex( x, y );
		CONFIG::debugging{
			trace( this + ".calculateDropIndex>>> rt:" + rt + ", y:" + y + ", firstIndexInView:" + firstIndexInView);
		}
		_dropIndicatorInsideIndex = -1;
		
		var dropIndexRect:Rectangle = getElementBounds( rt );
		var preDropIndexRect:Rectangle = getElementBounds( rt - 1 );
		
		if( dropIndexRect != null && preDropIndexRect == null ){
			CONFIG::debugging{
				if( rt > 0 ){
					throw new Error( "predropindexrect can't be null unless dropindex is 0" );
				}	
			}
			_dropIndicatorInsideIndex = 0;
		}
		else if( dropIndexRect != null && preDropIndexRect != null){
			CONFIG::debugging{
				trace( this + ".calculateDropIndex+++ preDropIndexRect.top:" 
					+ preDropIndexRect.top + ", y:" + y + ", dropIndexRect.bottom:" + dropIndexRect.bottom);
			}
			if( y - preDropIndexRect.top > dropIndexRect.bottom - y ){
				_dropIndicatorInsideIndex = rt;
			}
			else{
				_dropIndicatorInsideIndex = rt - 1;
			}
		}
		else if( dropIndexRect == null && preDropIndexRect != null ){
			CONFIG::debugging{
				trace( this + ".calculateDropIndex+++ preDropIndexRect.top:" 
					+ preDropIndexRect.top + ", y:" + y + ", preDropIndexRect.bottom:" + preDropIndexRect.bottom);
			}
			if( preDropIndexRect.top < y && y < preDropIndexRect.bottom ){
				_dropIndicatorInsideIndex = rt - 1;
			}
		}
		
		CONFIG::debugging{
			trace( this + ".calculateDropIndex<<< _dropIndicatorInsideIndex:" + _dropIndicatorInsideIndex );
		}
        return rt;
    }

    private var _dropIndicatorInsideIndex:int = -1;

    override public function calculateDropLocation(dragEvent:DragEvent):DropLocation{
        var sL:DropLocation =super.calculateDropLocation( dragEvent );
        var dropLocation:ListTreeDropLocation = new ListTreeDropLocation();
        dropLocation.dropAboveIndex = _dropIndicatorInsideIndex;
        dropLocation.dragEvent = dragEvent;
        dropLocation.dropPoint = sL.dropPoint;
        dropLocation.dropIndex = sL.dropIndex;

        return dropLocation;
    }
}
}